﻿using System;
using System.Linq;
using System.Data.Services.Client;
using System.Windows.Controls.Primitives;
using Coevery.ViewModel.EntityDataService;

namespace Coevery.ViewModel
{
    public class IndexViewModel : BaseViewModel
    {
        private bool _showAll;
        private Guid _userId;
        public Popup Popup { get; set; }
        public event EventHandler EnableButton;

        private DataServiceCollection<Lead> _leads;

        public DataServiceCollection<Lead> Leads
        {
            get{return _leads;}
            set
            {
                _leads = value;
                _leads.LoadCompleted += LeadQueryComplete;
                OnPropertyChanged("Leads");
            }
        }

        private DataServiceCollection<Opportunity> _opportunities;

        public DataServiceCollection<Opportunity> Opportunities
        {
            get { return _opportunities; }
            set
            {
                _opportunities = value;
                _opportunities.LoadCompleted += OpportunityQueryComplete;
                OnPropertyChanged("Opportunities");
            }
        }

        private DataServiceCollection<Quote> _quotes;

        public DataServiceCollection<Quote> Quotes 
        {
            get { return _quotes; }
            set 
            {
                _quotes = value;
                _quotes.LoadCompleted += QuoteQueryComplete;
                OnPropertyChanged("Quotes");
            }
        }

        private DataServiceCollection<Contract> _contracts;

        public DataServiceCollection<Contract> Contracts 
        {
            get { return _contracts; }
            set 
            {
                _contracts = value;
                _contracts.LoadCompleted += ContractQueryComplete;
                OnPropertyChanged("Contracts");
            }
        }

        public void LoadData(bool all, Popup popup, Guid userId)
        {
            Leads = new DataServiceCollection<Lead>();
            Opportunities = new DataServiceCollection<Opportunity>();
            Quotes = new DataServiceCollection<Quote>();
            Contracts = new DataServiceCollection<Contract>();
            _showAll = all;
            _userId = userId;
            if (_showAll)
            {
                var query = (from cust in Context.Leads
                             where cust.IsDeleted == false
                             orderby cust.ModifiedOn descending
                             select new Lead
                             {
                                 LeadId = cust.LeadId,
                                 FullName = cust.FullName,
                                 Subject = cust.Subject,
                                 Country = cust.Country,
                                 ModifiedOn = cust.ModifiedOn,
                                 StatusCode = cust.StatusCode,
                                 Technology = cust.Technology,
                                 LeadSourceCode = cust.LeadSourceCode,
                             });

                Leads.LoadAsync(query);

            }
            else
            {
                var query = (from cust in Context.Leads
                             where cust.OwnerId == userId
                             && cust.IsDeleted == false
                             orderby cust.ModifiedOn descending
                             select new Lead
                             {
                                 LeadId = cust.LeadId,
                                 FullName = cust.FullName,
                                 Subject = cust.Subject,
                                 Country = cust.Country,
                                 ModifiedOn = cust.ModifiedOn,
                                 StatusCode = cust.StatusCode,
                                 Technology = cust.Technology,
                                 LeadSourceCode = cust.LeadSourceCode,
                             });

                Leads.LoadAsync(query);
                
            }
            Popup = popup;
        }

        private void LeadQueryComplete(object sender, LoadCompletedEventArgs e)
        {
            if (Leads.Continuation != null)
            {
                Leads.LoadNextPartialSetAsync();
            }
            if (_showAll)
            {
 
                var opportunities = from cust in Context.Opportunities.Expand("Customer")
                            where cust.IsDeleted == false
                            orderby cust.ModifiedOn descending
                            select cust;

                Opportunities.LoadAsync(opportunities);
            }
            else
            {

                var opportunities = from cust in Context.Opportunities.Expand("Customer")
                            where cust.OwnerId == _userId &&
                            cust.IsDeleted == false
                            orderby cust.ModifiedOn descending
                            select cust;

                Opportunities.LoadAsync(opportunities);

            }

        }

        private void OpportunityQueryComplete(object sender, LoadCompletedEventArgs e)
        {   
            if (Opportunities.Continuation != null)
            {
                Opportunities.LoadNextPartialSetAsync();
            }
            if (_showAll)
            {
                var quotes = from quote in Context.Quotes.Expand("Owner")
                             where quote.IsDeleted == false
                             orderby quote.ModifiedOn descending
                             select quote;
                Quotes.LoadAsync(quotes);
            }
            else 
            {
                var quotes = from quote in Context.Quotes.Expand("Owner")
                             where quote.OwnerId == _userId &&
                             quote.IsDeleted == false
                             orderby quote.ModifiedOn descending
                             select quote;
                Quotes.LoadAsync(quotes);
            }
        }

        private void QuoteQueryComplete(object sender,LoadCompletedEventArgs e) 
        {
            if (Quotes.Continuation != null) 
            {
                Quotes.LoadNextPartialSetAsync();
            }
            if (_showAll)
            {
                var contracts = (from contract in Context.Contracts
                                 where contract.IsDeleted == false
                                 orderby contract.ModifiedOn descending
                                 select new Contract
                                 {
                                     ContractId = contract.ContractId,
                                     StatusCode = contract.StatusCode,
                                     ModifiedOn = contract.ModifiedOn,
                                     BillingCustomer = contract.BillingCustomer,
                                     Title = contract.Title,
                                 }
                 );
                Contracts.LoadAsync(contracts);
            }
            else 
            {
                var contracts = (from contract in Context.Contracts
                                 where contract.OwnerId == _userId
                                 && contract.IsDeleted == false
                                 orderby contract.ModifiedOn descending
                                 select new Contract
                                 {
                                     ContractId = contract.ContractId,
                                     StatusCode = contract.StatusCode,
                                     ModifiedOn = contract.ModifiedOn,
                                     BillingCustomer = contract.BillingCustomer,
                                     Title = contract.Title,
                                 }
    );
                Contracts.LoadAsync(contracts);
            }
        }
    
        private void ContractQueryComplete(object sender, LoadCompletedEventArgs e) 
        {
          if(Contracts.Continuation!=null)
          {
              Contracts.LoadNextPartialSetAsync();
          }
          Popup.IsOpen = false;
          EnableButton.Invoke(true, null);
        }
    }
}
